﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace OsuKeyboard
{
	public partial class KeySettingsPanel : Form
    {
        public KeySettingsPanel()
        {
            InitializeComponent();
        }
		//定义组合键值变量
		private string[] ComValue = new string[9] { "", "", "", "", "", "", "", "", "" };
        //初始化屏幕键盘
        private void KeySettingsPanel_Load(object sender, EventArgs e)
        {
            string selectBT = "";
            FormMain mainForm = (FormMain)this.Owner;
            if (mainForm.SelectedBT.Name.Contains("Button1")) selectBT = "BT1";
            if (mainForm.SelectedBT.Name.Contains("Button2")) selectBT = "BT2";
            if (mainForm.SelectedBT.Name.Contains("Button3")) selectBT = "BT3";
            if (mainForm.SelectedBT.Name.Contains("Button4")) selectBT = "BT4";
            if (mainForm.SelectedBT.Name.Contains("Button5")) selectBT = "BT5";
            mainForm.TKValue = "None";
            this.Text = Languages.GetLang("KeySettingsPanel") + "-" + selectBT;
            AddMouseUpEvent();
        }
        //添加鼠标弹起事件
        private void AddMouseUpEvent()
        {
            foreach (Button TKey in KeyboardPanel.Controls)
            {
                TKey.MouseUp += new MouseEventHandler(TK_MouseUp);
            }
        }

        //鼠标弹起事件处理
        private void TK_MouseUp(object sender, MouseEventArgs e)
        {
            Button Tkey = sender as Button;
            FormMain mainForm = (FormMain)this.Owner;
            if (e.Button == MouseButtons.Right)
            {
				if (Tkey.Text == "Ctrl" || Tkey.Text == "Alt" || Tkey.Text == "Shift" || Tkey.Text == "Win")
				{
					ComValue[8] = Tkey.Name;
				}
                else if (Tkey.TextAlign == ContentAlignment.MiddleCenter)
                {
                    string NumPadStr = "0123456789./*-+";
                    if (NumPadStr.Contains(Tkey.Text))
                    {
						ComValue[8] = "Num(" + Tkey.Text + ")";
                    }
                    else
                    {
						ComValue[8] = Tkey.Text;
                    }
                }
                else
                {
					ComValue[8] = Tkey.Text;
                }
            }
            if (e.Button == MouseButtons.Left)
			{
				Tkey.BackColor = Tkey.BackColor.ToArgb() == Color.Peru.ToArgb() ? Color.FromArgb(64, 64, 64) : Color.Peru;
				if (Tkey.Text == "Ctrl")
				{
					if (Tkey.Name == "LCtrl")
					{
						ComValue[0] = ComValue[0] == "" ? Tkey.Name : "";
					}
					else
					{
						ComValue[4] = ComValue[4] == "" ? Tkey.Name : "";
					}
                    return;
                }
                if (Tkey.Text == "Alt")
                {
					if (Tkey.Name == "LAlt")
					{
						ComValue[1] = ComValue[1] == "" ? Tkey.Name : "";
					}
					else
					{
						ComValue[5] = ComValue[5] == "" ? Tkey.Name : "";
					}
                    return;
                }
                if (Tkey.Text == "Shift")
				{
					if (Tkey.Name == "LShift")
					{
						ComValue[2] = ComValue[2] == "" ? Tkey.Name : "";
					}
					else
					{
						ComValue[6] = ComValue[6] == "" ? Tkey.Name : "";
					}
					return;
                }
                if (Tkey.Text == "Win")
				{
					if (Tkey.Name == "LWin")
					{
						ComValue[3] = ComValue[3] == "" ? Tkey.Name : "";
					}
					else
					{
						ComValue[7] = ComValue[7] == "" ? Tkey.Name : "";
					}
					return;
                }
                if (Tkey.TextAlign == ContentAlignment.MiddleCenter)
                {
                    string NumPadStr = "0123456789./*-+";
                    if (NumPadStr.Contains(Tkey.Text))
                    {
                        ComValue[8] = "Num(" + Tkey.Text + ")";
                    }
                    else
                    {
                        ComValue[8] = Tkey.Text;
                    }
                }
                else
                {
                    ComValue[8] = Tkey.Text;
                }
            }
			mainForm.TKValue = string.Join("|", ComValue);
			this.Close();
		}
    }
}
